package com.wkt.ggkt.vod.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcloud.vod.common.StringUtil;
import com.wkt.ggkt.model.vod.Teacher;
import com.wkt.ggkt.result.Result;
import com.wkt.ggkt.vo.vod.TeacherQueryVo;
import com.wkt.ggkt.vod.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wkt
 * @since 2022-06-29
 */
@Api(tags = "讲师管理控制器")
@RestController
@RequestMapping("/admin/vod/teacher")
//@CrossOrigin
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation("查询所有讲师")
    @GetMapping("/getAllTeacher")
    public Result getAllTeacher() {
        List<Teacher> list = teacherService.list();
        return Result.ok(list);
    }

    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("/deleteTeacher/{id}")
    public Result deleteTeacher(@ApiParam(name = "id",value = "id",required = true)
                                     @PathVariable("id") Long id){
        boolean b = teacherService.removeById(id);
        return Result.ok(b);
    }

    @ApiOperation("分页查询")
    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody TeacherQueryVo teacherQueryVo){

        Page<Teacher> pageParam = new Page<Teacher>(teacherQueryVo.getPageNo(),teacherQueryVo.getPageSize());
        IPage<Teacher> pageModel;
        if (teacherQueryVo == null){
            pageModel = teacherService.page(pageParam, null);
        }else {
            QueryWrapper<Teacher> queryWrapper = new QueryWrapper<Teacher>();
            if (!StringUtil.isEmpty(teacherQueryVo.getName())){
                queryWrapper.like("name",teacherQueryVo.getName());
            }
            if (teacherQueryVo.getLevel() != null){
                queryWrapper.eq("level", teacherQueryVo.getLevel());
            }
            if (!StringUtil.isEmpty(teacherQueryVo.getJoinDateBegin())){
                queryWrapper.ge("join_date",teacherQueryVo.getJoinDateBegin());
            }
            if (!StringUtil.isEmpty(teacherQueryVo.getJoinDateEnd())){
                queryWrapper.le("join_date",teacherQueryVo.getJoinDateEnd());
            }
            pageModel = teacherService.page(pageParam, queryWrapper);
        }
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("/saveTeacher")
    public Result saveTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.save(teacher);
        if (isSuccess) {
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/getTeacherById/{id}")
    public Result getTeacherById(@PathVariable("id") Long id){
        Teacher teacher = teacherService.getById(id);
        if (teacher == null) {
            return Result.fail(null);
        }else {
            return Result.ok(teacher);
        }
    }

    @ApiOperation(value = "修改讲师信息")
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.updateById(teacher);
        if (isSuccess) {
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    @ApiOperation(value = "批量删除讲师")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody Set<Long> idList){
        boolean isSuccess = teacherService.removeByIds(idList);
        if (isSuccess) {
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }
}

